package pt.inescid.components.policyengine.heimdall.database;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import pt.inescid.components.policyengine.heimdall.base.User;
import pt.inescid.components.policyengine.heimdall.commons.DBManager;
import pt.inescid.components.policyengine.heimdall.commons.Log;

public class UserDAO {
	
	private static Vector cachedUsers = null;

	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 ============= getRecord =====================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	public static User getRecord(long userId) {
		
		ResultSet rs;
		User currentRecord;
		
		String query = "SELECT * FROM user WHERE id=" + userId + ";";
		
		try {
			rs = DBManager.executeQuery(query);
			
			if (rs.first()) {
				currentRecord = createRecord(rs);
			}
			else {
				currentRecord = null;
			};
			
		}
		catch (SQLException e) {
			Log.debug(Log.ERROR, "Erro SQL: " + e + 
					" - query = " + query, "UserDAO.getRecord");
			currentRecord = null;
		}
		
		return (currentRecord);
	}; // getRecord
	
	
	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 =============      createRecord   =============================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	
	public static User createRecord(ResultSet rs)
	throws SQLException {
		User newRecord = null;
		
		try {
			newRecord = new User(
					rs.getLong("id"),
					rs.getString("name"));
		}
		catch (Exception e) {
			Log.debug(Log.ERROR, "Erro SQL: " + e, "UserDAO.createRecord");
			newRecord = null;
		}
		
		return (newRecord);
	}; // createRecord
	

	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 ============= insertRecord =====================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	public static void insertRecord(long currentUserId, String policyText) {
		String query = "INSERT INTO policy(user,info) VALUES (?,?);";
		
		PreparedStatement ps;
		try {
			ps = DBManager.getConnection().prepareStatement(query);
			ps.setLong(1, currentUserId);
			ps.setString(2, policyText);
			ps.execute();
			ps.close();
		} catch (SQLException e) {
			Log.debug(Log.INFO, "SQL Problem", 
			"PolicyUploadFC.loadPolicy"); 	
		}
	}
	
	
	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 ============= getAllRecords =====================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	public static Vector getAllRecords() {
		
		ResultSet rs;
		Vector allUsers = new Vector();
		
		String query = "SELECT * FROM user;";
		
		try {
			rs = DBManager.executeQuery(query);
			
			if (rs.first()) {
				do {
					allUsers.add(createRecord(rs));
				} while (rs.next());
			}
			
		}
		catch (SQLException e) {
			Log.debug(Log.ERROR, "Erro SQL: " + e + 
					" - query = " + query, "EventObjectDAO.getAllRecordsAfter");
			allUsers = null;
		}
		
		return (allUsers);
	}; // getAllRecords
	
	


	public static void main(String s[]) {
		Vector a = getAllRecords();
		a = null;
	}
	
}; // PolicyDAO
